package com.app.controller;

import com.app.common.Result;
import com.app.model.AnimalMapModel;
import com.app.server.AnimalMapService;
import com.app.utils.CommonHelp;
import com.app.vo.AnimalMapVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 野生动物地图
 */
@Slf4j
@RestController
@RequestMapping("/sbbz/animalMap")
public class AnimalMapController {
    @Autowired
    private AnimalMapService animalMapService;

    // 查询列表
    @GetMapping("/getAnimalMapList")
    public Result getAnimalMapList(@RequestParam(defaultValue = "0") Integer page,
                                   @RequestParam(defaultValue = "10") Integer limit,String keyword) {
        AnimalMapVo animalMapVo = animalMapService.getAnimalMapList(page, limit,keyword);
        return Result.success(animalMapVo);
    }

    //根据id查询
    @GetMapping("/getAnimalMapById")
    public Result getAnimalMapById(Integer id) {
        AnimalMapModel animalMap = animalMapService.getById(id);
        return Result.success(animalMap);
    }

    //新增和修改
    @PostMapping("/addAndUpdateAnimalMap")
    public Result addAndUpdateAnimalMap(@RequestBody AnimalMapModel animalMapModel) {
        AnimalMapModel animalMap = animalMapService.getById(animalMapModel.getId());
        if (animalMap != null) {
            animalMapModel.setUpdateTime(CommonHelp.getNowTimestamp());
            animalMapService.updateById(animalMapModel);
            return Result.success("修改成功");
        } else {
            animalMapModel.setCreateTime(CommonHelp.getNowTimestamp());
            animalMapService.save(animalMapModel);
            return Result.success("添加成功");
        }
    }
    //根据id删除
    @GetMapping("/delAnimalMap")
    public Result delAnimalMap(Integer id) {
        animalMapService.removeById(id);
        return Result.success("删除成功");
    }

}
